package com.j1903.util;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;

/**
 * 发送邮件类
 */
public class SendEmail {
    /**
     * 发送
     * @param recEmail 收件人邮箱
     * @param subject 邮件标题
     * @param content 邮件内容
     * @param contentType 内容类型,如"text/plain;charset=UTF-8"
     * @return true/false
     */
    public static Boolean send(
            String recEmail,String subject,
            String content,String contentType
    ){
        try{
            //创建一个邮件配置对象
            Properties props=new Properties();
            //开始smtp身份验证
            props.put("mail.smtp.auth", true);
            //设置smtp邮件服务器地址
            props.put("mail.smtp.host","smtp.qq.com");
            //设置邮件服务的端口
            props.put("mail.smtp.port","587");
            //设置开发者的qq邮箱
            props.put("mail.user","2232022840@qq.com");
            //设置qq邮箱授权码
            props.put("mail.password","xalocdjvdiudebgi");
            //构建授权信息对象
            Authenticator auth=new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    String userName=props.getProperty("mail.user");
                    String passWord=props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, passWord);
                }
            };
            //创建邮件会话
            Session mailSession=
                    Session.getInstance(props,auth);
            //创建邮件信息对象
            MimeMessage msg=new MimeMessage(mailSession);
            //设置发件人
            InternetAddress form=
                    new InternetAddress(
                            (String) props.get("mail.user"));
            msg.setFrom(form);
            //设置收件人
            InternetAddress to=
                    new InternetAddress(recEmail);
            msg.setRecipient(Message.RecipientType.TO,to);
            //设置邮件标题
            msg.setSubject(subject);
            //邮件内容
            msg.setContent(content,contentType);
            //发送邮件
            Transport.send(msg);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 发送验证码
     * @param email 收件人邮箱
     * @return
     */
    public static String sendAuthCode(String email){
        StringBuffer sb=new StringBuffer();
        for (int i = 0; i < 6; i++) {
            sb.append(new Random().nextInt(10));
        }
        boolean rs= SendEmail.send(email,
                "注册验证码","【J1903】发送的注册验证码:<h1>"
                        +sb.toString()+"</h1>",
                "text/html;charset=UTF-8");
        if(rs){
            Map<String,Object> param=new HashMap<>();
            param.put("code", sb.toString());
            return JwtUtil.createToken("",10,param);
        }
        return null;
    }
}
